package com.fengke.util.master_slave;

import org.springframework.util.Assert;

public class DataSourceSwitch {
	@SuppressWarnings("rawtypes")
	private static final ThreadLocal contextHolder = new ThreadLocal();

	@SuppressWarnings("unchecked")
	public static void setDataSource(String dataSource) {
		Assert.notNull(dataSource, "数据源未创建成功!");
		contextHolder.set(dataSource);
	}

	@SuppressWarnings("unchecked")
	public static void setMaster() {
		contextHolder.remove();
		contextHolder.set("master");
	}

	@SuppressWarnings("unchecked")
	public static void setSlave() {
		contextHolder.remove();
		contextHolder.set("slave");
	}

	public static String getDataSouce() {
		if(contextHolder.get() == null) {
			setMaster();
		}
		return contextHolder.get().toString();
	}

}
